﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CardLogistics.Web.MainPage.SystemMgr.SysRole
{
    public partial class AuthMenu : System.Web.UI.Page
    {

        protected DataSet ds = new DataSet();
        private static DataTable dt = new DataTable();
        protected string RoleName = "";
        private static CardLogistics.DAL.Sys.Sys_SysRolesData DAL = new DAL.Sys.Sys_SysRolesData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //已经定义好的数据
                RoleName = Server.UrlDecode(Request.QueryString["rname"]);
                string rId = Request.QueryString["rid"];
                dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(" select * from sys_menus where status=1 order by sort ");
                DataTable dtM = CardLogistics.DAL.DbHelperSQL.GetQueryResult(" select * from sys_sysrolemenus where roleid='" + rId + "' ");
                ds.Tables.Add(dt);
                CreateTree(this.TreeView1.Nodes, "", dtM);
                TreeView1.Attributes.Add("onclick", "CheckEvent(event)");

            }
        }
        /// <summary>
        /// 绑定树列表
        /// </summary>
        /// <param name="TNCs">父节点</param>
        /// <param name="strParentId">父节点id</param>
        private void CreateTree(TreeNodeCollection TNCs, string strParentId, DataTable dtM)
        {
            TreeNode tmpTNCs = new TreeNode();
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            string strID;
            if (strParentId == "")
            {
                dv.RowFilter = " isnull(PARENTID,0)=0";
            }
            else
            {
                dv.RowFilter = " PARENTID = " + strParentId;
            }
            foreach (DataRowView drv in dv)
            {
                tmpTNCs = new TreeNode();//定义结点
                strID = drv["MENUID"].ToString();
                tmpTNCs.Value = strID; //定义结点ID
                tmpTNCs.Text = drv["MENUNAME"].ToString(); //选择不触发事件
                TNCs.Add(tmpTNCs);//添加结点
                tmpTNCs.CollapseAll();
                if (dtM.Select(" menuid='" + strID + "' ").Length > 0)
                    tmpTNCs.Checked = true;
                else
                    tmpTNCs.Checked = false;
                CreateTree(TNCs[TNCs.Count - 1].ChildNodes, strID, dtM);//递归调用
            }
        }

        protected void btn_Sure_Click(object sender, EventArgs e)
        {
            string nTxt = "";
            GetAllNodeText(TreeView1.Nodes, ref nTxt);
            if (nTxt.Length > 0)
            {
                string rId = Request.QueryString["rid"];
                int result = DAL.RolesAddMenu(rId, nTxt.TrimEnd(',').Split(','));
                if (result > 0)
                {
                    Response.Write("<script Language=\"Javascript\">");
                    Response.Write("alert(\"授权成功！\");window.close();");
                    Response.Write("</script>");
                    Response.End();
                }
            }
            else
            {
                Response.Write("<script Language=\"Javascript\">");
                Response.Write("alert(\"请选择该角色拥有的权限！\");location.href=location.href;");
                Response.Write("</script>");
                Response.End();
            }
        }

        /// <summary>
        /// 获取树列表选中的值
        /// </summary>
        /// <param name="tnc"></param>
        /// <param name="str"></param>
        private void GetAllNodeText(TreeNodeCollection tnc, ref string str)
        {
            foreach (TreeNode node in tnc)
            {
                if (node.Checked == true)
                {
                    str += node.Value + ",";
                    GetAllNodeText(node.ChildNodes, ref str);
                }
            }
        }
    }
}